package com.threeidiots.findit.db;

import java.util.ArrayList;

import com.threeidiots.findit.db.FindItDatabase.RecordsCursor;

/**
 * Entity of Record field
 * @author Hemslo
 *
 */
public class Record {
	private FindItDatabase db;
	private long _id;
	private String thing;
	private Double latitude;
	private Double longitude;
	private String note;
	private String tag;
	private String picture;
	private String tape;
	private String place_time;
	private String pick_time;
	private boolean history;

	public Record(FindItDatabase db) {
		this.db = db;
		this._id = 0;
		this.thing = null;
		this.latitude = null;
		this.longitude = null;
		this.note = null;
		this.tag = null;
		this.picture = null;
		this.tape = null;
		this.place_time = null;
		this.pick_time = null;
		this.history = false;
	}
	public Record(FindItDatabase db,long _id, String thing, Double latitude, Double longitude,
			String note, String tag, String picture, String tape,
			String place_time, String pick_time, boolean history) {
		this.db = db;
		this._id = _id;
		this.thing = thing;
		this.latitude = latitude;
		this.longitude = longitude;
		this.note = note;
		this.tag = tag;
		this.picture = picture;
		this.tape = tape;
		this.place_time = place_time;
		this.pick_time = pick_time;
		this.history = history;
	}

	public void update() {
		this.db.editRecord(_id, thing, latitude, longitude, note, tag,
				picture, tape, place_time, pick_time, history ? 1 : 0);
	}

	public void insert() {
		this.db.addRecord(thing, latitude, longitude, note, tag, picture,
				tape, place_time, pick_time, history ? 1 : 0);
	}

	public void delete() {
		this.db.deleteRecord(_id);
	}

	public static void clear(FindItDatabase db, boolean history) {
		db.clearRecord(history);
	}

	public static int recordCount(FindItDatabase db) {
		return db.getRecordsCount(false);
	}

	public static int historyCount(FindItDatabase db) {
		return db.getRecordsCount(true);
	}

	/**
	 * 
	 * @param db
	 * @param history history determine whether get history
	 * @return
	 */
	public static ArrayList<Record> getRecords(FindItDatabase db,boolean history) {
		ArrayList<Record> records = new ArrayList<Record>();
		RecordsCursor cursor = db.getRecords(history);
		while (!cursor.isAfterLast()) {
			records.add(new Record(
					db,
					cursor.getColRecordsId(),
					cursor.getColThing(),
					cursor.getColLatitude(),
					cursor.getColLongitude(),
					cursor.getColNote(),
					cursor.getColTag(),
					cursor.getColPicture(),
					cursor.getColTape(),
					cursor.getColPlaceTime(),
					cursor.getColPickTime(),
					history));
			cursor.moveToNext();
		}
		cursor.close();
		return records;
	}
	
	public static ArrayList<Record> searchRecords(FindItDatabase db,String pattern,boolean history) {
		ArrayList<Record> records = new ArrayList<Record>();
		RecordsCursor cursor = db.searchReords(pattern, history);
		while (!cursor.isAfterLast()) {
			records.add(new Record(
					db,
					cursor.getColRecordsId(),
					cursor.getColThing(),
					cursor.getColLatitude(),
					cursor.getColLongitude(),
					cursor.getColNote(),
					cursor.getColTag(),
					cursor.getColPicture(),
					cursor.getColTape(),
					cursor.getColPlaceTime(),
					cursor.getColPickTime(),
					history));
			cursor.moveToNext();
		}
		cursor.close();
		return records;
	}
	
	public long getId() {
		return _id;
	}

	public void setId(long _id) {
		this._id = _id;
	}

	public String getThing() {
		return thing;
	}

	public void setThing(String thing) {
		this.thing = thing;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getTape() {
		return tape;
	}

	public void setTape(String tape) {
		this.tape = tape;
	}

	public String getPlaceTime() {
		return place_time;
	}

	public void setPlaceTime(String place_time) {
		this.place_time = place_time;
	}

	public String getPickTime() {
		return pick_time;
	}

	public void setPickTime(String pick_time) {
		this.pick_time = pick_time;
	}

	public boolean isHistory() {
		return history;
	}

	public void setHistory(boolean history) {
		this.history = history;
	}

}
